package io.terminus.spzx.utils;

import io.terminus.spzx.model.entity.system.SysUser;
import io.terminus.spzx.model.entity.user.UserInfo;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc ThreadLocal工具类
 * @Date 2024-03-25
 */
public class AuthContextUtil {

    private static final ThreadLocal<UserInfo> userInfoThreadLocal = new ThreadLocal<>();

    /**
     * 定义存储数据的静态方法
     *
     * @param userInfo 用户信息
     */
    public static void setUserInfo(UserInfo userInfo) {
        userInfoThreadLocal.set(userInfo);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    public static UserInfo getUserInfo() {
        return userInfoThreadLocal.get();
    }

    /**
     * 获取用户ID
     *
     * @return 用户ID
     */
    public static Long getUserId() {
        return userInfoThreadLocal.get().getId();
    }

    /**
     * 删除数据的方法
     */
    public static void removeUserInfo() {
        userInfoThreadLocal.remove();
    }

    /**
     * 创建threadLocal对象
     */
    private static final ThreadLocal<SysUser> threadLocal = new ThreadLocal<>();

    /**
     * 添加数据: 用户
     *
     * @param sysUser 用户信息
     */
    public static void set(SysUser sysUser) {
        threadLocal.set(sysUser);
    }

    /**
     * 获取数据
     *
     * @return 用户信息
     */
    public static SysUser get() {
        return threadLocal.get();
    }

    /**
     * 删除数据
     */
    public static void remove() {
        threadLocal.remove();
    }
}
